/**
 * Created by ercha on 2016/1/1.
 */

$(document).ready(function(){

    //上传展示图片
    var imageList = [], images="";
    var idx = 0;

    var cover = "";

    $('#images').on('fileuploaded', function(event, data, previewId, index, reader){
        var url = data.response.url;
        console.info("images: " + url);
        imageList.push(url);
        idx++;
        console.info("imageList: " + imageList[idx-1]);
    });

    //上传完展示图片后上传表单
    $('#images').on('filebatchuploadcomplete', function(event, files, extra){
        console.info("here images complete!");
        images = makeJSONStr(imageList);
        console.info("images: " + images);
        submitForm(cover, images);
        $('#images').fileinput('clear');
        $('#images').fileinput('reset');
    });

    //发送完的回调函数
    $('#cover').on('fileuploaded', function(event, data, previewId, index, reader){
        cover = data.response.url;

        //如果需要上传其他展示图片，则在那里提交表单
        if($('#images').val()!=null && $('#images').val()!=""){
            $('#images').fileinput('upload');
        } else{
            submitForm(cover, null);
        }
        $('#cover').fileinput('clear');
        $('#cover').fileinput('reset');
        $('#cover').fileinput('enable');
    });

    $('#createForm').validate({
        rules:{
            cover: {
                required: true
            },
            name: {
                required: true
            },
            isbn: {
                required: true,
                number: true
            },
            author: {
                required: true
            },
            language: {
                required: true
            },
            publishingHouse: {
                required: true
            },
            publicationYear: {
                required: true,
                number: true
            },
            price: {
                required: true,
                number: true
            }
        },
        errorPlacement: errorPlacement,
        success: success,
        submitHandler: function(form){
            waitMessage();
            images = cover = "";
            imageList = [];
            idx = 0;
            $('#cover').fileinput('upload');
        }
    });
});

function submitForm(cover, images){
    $.ajax({
        url: path + "/book/update",
        type: "POST",
        dataType: "json",
        data: {
            id: $('#id').val(),
            cover: cover,
            name: $('#name').val(),
            isbn: $('#isbn').val(),
            author: $('#author').val(),
            language: $('#language').val(),
            publishingHouse: $("#publishingHouse").val(),
            publicationYear: $("#publicationYear").val(),
            price: $("#price").val(),
            status: $("#status").val() ? 1 : 0,
            remark: $("#remark").val(),
            images: images
        },
        success: function(data){
            if(data.success == true){
                successMessage("操作成功！");
            } else{
                errorMessage("操作失败！");
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            errorMessage("操作失败！错误代码：" + XMLHttpRequest.status);
        }
    });
}